package com.tuojiahr.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.Preparable;
import com.tuojiahr.common.BaseActionCommon;
import com.tuojiahr.domain.TuoJiaHrOrdinaryColumn;
import com.tuojiahr.domain.TuoJiaHrUser;
import com.tuojiahr.global.Constants;
import com.tuojiahr.pagemodel.PageModel;
import com.tuojiahr.service.OrdinaryColumnService;
import com.tuojiahr.tools.Pager;
import com.tuojiahr.tools.StringUtil;

/**
 * 说明：
 *
 * @author wang.haibo
 * @date 2012-11-7 下午7:35:46
 */

public class OrdinaryColumnAction extends BaseActionCommon implements Preparable {

	private static final long serialVersionUID = 1L;
	private OrdinaryColumnService ordinaryColumnService; 
	
	private PageModel pageModel;
	private TuoJiaHrOrdinaryColumn ordinaryColumn;
	private Pager pager;
	
	private String keyWord;
	private String url = "adminmgr/oc_ordinaryColumnList";
	
	private String message = "出错了！！！";
	
	private TuoJiaHrUser user = null;
	
	@Override
	public void prepare() throws Exception {
		user = getCurrUser();
	}
						
	/**
	 * 分页获取普通栏目，支持根据名称模糊查询
	 * @return
	 */
	public String ordinaryColumnList() {
		if(null != user) {
			try {
				pageModel = ordinaryColumnService.getOrdinaryColumn(keyWord);
				return "ordinary_view";
			} catch (Exception e) {
				Constants.ExceptionLog.error("##OrdinaryColumnAction---ordinaryColumnList查询出错:", e);
			}
		} 
		return "msg";
	}
	
	/**
	 * 根据id查看一条记录
	 * @return
	 */
	public String openOneView() {
		if(null != user) {
			String ordinaryId = request.getParameter("ordinaryId");
			if(StringUtil.isNotBlank(ordinaryId)) {
				try {
					ordinaryColumn = ordinaryColumnService.getOrdinaryColumnById(Long.valueOf(ordinaryId));
					return "one_ordinary_view";
				} catch (Exception e) {
					Constants.ExceptionLog.error("##OrdinaryColumnAction---openOneView方法查询出错:", e);
				}
			}
		}
		return "msg";
	}
	
	/**
	 * 打开添加或更新窗口
	 * @return
	 */
	public String openAddOrUpdate() {
		if(null != user) {
			String ordinaryId = request.getParameter("ordinaryId");
			if(StringUtil.isNotBlank(ordinaryId)) {
				ordinaryColumn = ordinaryColumnService.getOrdinaryColumnById(Long.valueOf(ordinaryId));
				url = "adminmgr/oc_updateOrdinaryColumn";
			} else {
				url = "adminmgr/oc_addOrdianryColumn";
			}
			//是否主页显示
			Map<String, String> selectHomeShow = new HashMap<String, String>();
			selectHomeShow.put("y", "显示");
			selectHomeShow.put("n", "不显示");
			request.setAttribute("selectHomeShow", selectHomeShow);
			return "add_update_ordinary";
		} 
		return "msg";
	}
	
	/**
	 * 添加
	 */
	public String addOrdianryColumn() {
		if(null != user) {
			ordinaryColumn.setOperator(user);
			try {
				ordinaryColumnService.addOrdinaryColumn(ordinaryColumn);
				return "action";
			} catch (Exception e) {
				Constants.ExceptionLog.error("##OrdinaryColumnAction---addOrdianryColumn方法添加出错:", e);
			}
		} 
		return "msg";
	}
	
	/**
	 * 编辑
	 */
	public String updateOrdinaryColumn() {
		if(null != user) {
			if(null != ordinaryColumn && 0 != ordinaryColumn.getId()) {
				ordinaryColumn.setOperator(user);
				try {
					ordinaryColumnService.updateOrdinaryColumn(ordinaryColumn);
					return "action";
				} catch (Exception e) {
					Constants.ExceptionLog.error("##OrdinaryColumnAction---updateOrdinaryColumn方法更新出错:", e);
				}
			}
		}
		return "msg";
	}
	
	/**
	 * 删除，支持批量
	 * @throws IOException 
	 */
	public void delOrdinaryById() throws IOException {
		String result = "fault";
		String ids = request.getParameter("ids");
		if(StringUtil.isNotBlank(ids)) {
			try {
				ordinaryColumnService.deleteOrdinaryColumn(ids);
				result = "success";
			} catch (Exception e) {
				result = "error";
				Constants.ExceptionLog.error("##OrdinaryColumnAction---delOrdinaryById方法删除出错:", e);
			}
		}
		response.getWriter().print(result);
		response.getWriter().close();
	}

	public PageModel getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}

	public void setOrdinaryColumnService(OrdinaryColumnService ordinaryColumnService) {
		this.ordinaryColumnService = ordinaryColumnService;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public TuoJiaHrOrdinaryColumn getOrdinaryColumn() {
		return ordinaryColumn;
	}

	public void setOrdinaryColumn(TuoJiaHrOrdinaryColumn ordinaryColumn) {
		this.ordinaryColumn = ordinaryColumn;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
